<?php
class TemplateAction extends CommonAction {
	function index(){
		$this->listmap=1;
        parent::_list();
		$this->display();
	}
	
	function update(){
		if($this->_post('template_id')){
			if($this->_post('id')){
				$s=array();
				$setting_list=M('Setting_group')->order('sort')->select();
				foreach($setting_list as $k=>$v){
					$_select=M('Setting')->where(array('group_id'=>$v['id'],'id'=>array('in',$this->_post('id'))))->order('sort')->select();
					foreach($_select as $kk=>$vv){
						$_select[$kk]['_select']=json_decode($vv['_select'],true);
					}//dump($_select);
					$setting_list[$k]['list']=$_select;
					if(empty($_select)){
						unset($setting_list[$k]);
					}else{
						$s[]=$setting_list[$k];
					}
				}
				$data=JSON($s);
				//写入模板配置列表值
				//echo $data;
				$udata['config']=$data;
				$umap['id']=$this->_post('template_id');
				if(false!==$this->dao->where($umap)->data($udata)->save()){
					$this->success('设置配置项成功',U('Template/edit',array('id'=>$this->_post('template_id'))));
				}else{
					$this->error('出错了,请检查页隐藏域是否正常或其它程序代码是否被更改');
				}
				
		    }
		}else{
			parent::update();
		}
	}
	public function add(){
		$this->group_list=M('Template_group')->order('sort')->select();
		parent::add();
	}
	public function edit(){
		$this->group_list=M('Template_group')->order('sort')->select();
		parent::edit();
	}
	
	//预浏览配置页
	public function showConfView(){
		if($this->_get('id')){
			$ConfList=M("Template")->where(array('id'=>$this->_get('id')))->find();
			$this->setting_list=json_decode($ConfList['Conf'],true);
			$this->display();
		}
	}
}
?>